package com.onenine.app.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.onenine.app.R;
import com.onenine.app.utils.RoleResLoader;
import com.onenine.common.util.ApUtils;

import java.util.ArrayList;
import java.util.List;

public class GameSeekBar extends FrameLayout implements View.OnClickListener {

    private static final int UN_SELECT_INDICATOR_COLOR = 0XFF434955;
    private SeekBar mSeekBar;
    private List<TextView> mIndicatorList;
    private int mSelectValue;

    public GameSeekBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initViews(context);
    }

    public GameSeekBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews(context);
    }

    private void initViews(Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_game_seekbar, this, true);
        LinearLayout llIndicator = findViewById(R.id.ll_indicator);
        mSeekBar = findViewById(R.id.seek_bar);
        mSeekBar.setEnabled(false);
        mSeekBar.setProgressDrawable(ActivityCompat.getDrawable(ApUtils.getApp()
                , RoleResLoader.getRoleDrawableResourceId(RoleResLoader.DynamicResName.SHAPE_GAME_SEEKBAR_BG)));
        mIndicatorList = new ArrayList<>(llIndicator.getChildCount());
        for (int i = 0; i < llIndicator.getChildCount(); i++) {
            TextView vIndicator = (TextView) llIndicator.getChildAt(i);
            mIndicatorList.add(vIndicator);
            vIndicator.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        mSelectValue = Integer.parseInt(((TextView) v).getText().toString());
        int value = Integer.parseInt(v.getTag().toString());
        mSeekBar.setProgress(value);
        setIndicatorColor(v);
    }

    private void setIndicatorColor(View v) {
        int index = mIndicatorList.indexOf(v);
        for (int i = 0; i < mIndicatorList.size(); i++) {
            TextView vIndicator = mIndicatorList.get(i);
            vIndicator.setTextColor(i <= index ? RoleResLoader
                    .getRoleColorResourceId(RoleResLoader.DynamicResName.THEME_COLOR) : UN_SELECT_INDICATOR_COLOR);
            if (index == 0) {
                vIndicator.setTextColor(UN_SELECT_INDICATOR_COLOR);
            }
        }
    }

    public int getSelectValue() {
        return mSelectValue;
    }

}
